package com.china08.yxyapi.api.manage.stu.mystudy.homework.synchron;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 同步学生数据
 */
@CrossOrigin
@RestController
public class StuSynchronController extends YxyApiBaseController {

    private static final Log log = LogFactory.getLog(StuSynchronController.class);

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskReportQuesRepository HomeworkTaskReportQuesRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    // ##
    // 需要同步的表:
    // HomeworkTaskReport, HomeworkTaskReportQues
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/synchron", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess( //
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel //
    ) throws JsonParseException, JsonMappingException, IOException {
        // 参数校验
        if (reqModel.name == null || reqModel.name.trim().length() == 0) { return; }
        // 学生Id
        String studentId = getProfile(request, response).getId();
        // 获取当前在作业系统用户昵称叫什么
        HomeworkTaskReport homeworkTaskReport = homeworkTaskReportRepository.findTop1ByStudentId(studentId);
        if (homeworkTaskReport != null && !homeworkTaskReport.getStudentNick().equals(reqModel.name)) {
            try {
                // 更新条件
                Query updateQuery = new Query();
                updateQuery.addCriteria(Criteria.where("studentId").is(studentId));
                // 更新 HomeworkTaskReport
                Update update1 = new Update();
                update1.set("studentNick", reqModel.name);
                mongoTemplate.updateMulti(updateQuery, update1, HomeworkTaskReport.class);
                // 更新 HomeworkTaskReportQues
                Update update2 = new Update();
                update2.set("studentName", reqModel.name);
                mongoTemplate.updateMulti(updateQuery, update2, HomeworkTaskReportQues.class);
            } catch (Exception e) {
            }
        }
    }

    /** 请求的参数类型 **/
    protected static class ReqModel extends YxyApiReqModel {
        // 学生ID
        public String name;
    }
}
